#include"Stack.h"

typedef struct {
    ST* pushst;
    ST* popst;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
    queue->pushst = (ST*)malloc(sizeof(ST));
    queue->popst = (ST*)malloc(sizeof(ST));
    STInit(queue->pushst);
    STInit(queue->popst);
    return queue;
}

void myQueuePush(MyQueue* obj, int x) {
    assert(obj);
    STPush(obj->pushst, x);
}
bool myQueueEmpty(MyQueue* obj) {
    assert(obj);
    return STEmpty(obj->pushst) && STEmpty(obj->popst);
}
int myQueuePop(MyQueue* obj) {
    assert(obj && !myQueueEmpty(obj));
    if (STEmpty(obj->popst))
    {
        while (!STEmpty(obj->pushst))
        {
            STPush(obj->popst, STTop(obj->pushst));
            STPop(obj->pushst);
        }
    }
    int tem = STTop(obj->popst);
    STPop(obj->popst);
    return tem;

}

int myQueuePeek(MyQueue* obj) {
    assert(obj && !myQueueEmpty(obj));
    if (STEmpty(obj->popst))
    {
        while (!STEmpty(obj->pushst))
        {
            STPush(obj->popst, STTop(obj->pushst));
            STPop(obj->pushst);
        }
    }

    return STTop(obj->popst);
}



void myQueueFree(MyQueue* obj) {
    assert(obj);
    STDestroy(obj->popst);
    STDestroy(obj->pushst);
    free(obj);
    obj = NULL;
}